package com.gtq.uhome.controller;

import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.SuppliesManagementService;
import com.gtq.uhome.vo.SearchSupVo;
import com.gtq.uhome.vo.SuppliesVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 总站点物料管理
 */
@RestController
@RequestMapping(value = "/materialManagement")
@CrossOrigin
@Api(tags = "总站点物料管理")
public class MaterialManagementController {
    @Autowired
    private SuppliesManagementService suppliesManagementService;

    /**
     * 增加物料
     *
     * @param suppliesVO
     */
    @PutMapping
    @ApiOperation(value = "添加物料", notes = "添加物料信息")
    public Result addSupplies(@RequestBody SuppliesVO suppliesVO) {
        boolean b = suppliesManagementService.addSupplies(suppliesVO);
        if (b) {
            return Result.ok(null, "添加成功");
        }
        return Result.failed("添加失败，稍后再试");
    }

    /**
     * 修改物料信息
     *
     * @param suppliesVO 物料VO对象
     * @return 返回json格式的转转态
     */
    @PostMapping
    @ApiOperation(value = "修改物料信息", notes ="修改物料信息")
    public Result updateSupplies(@RequestBody SuppliesVO suppliesVO) {
        boolean b = suppliesManagementService.updateSupplies(suppliesVO);
        if (b) {
            return Result.ok(null, "修改成功");
        }
        return Result.failed("添加失败，稍后再试");
    }

    /**
     * 单个/批量删除物料信息
     *
     * @param suppLiesId 要删除物料ID
     * @return true 添加成功 false 添加失败
     */
    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除物料信息", notes = "删除指定物料信息")
    public Result deleteSupplies(@RequestBody Map<String,Map<String,Object>[]> suppLiesId) {
        List<Integer> siteIds=new ArrayList<>();
        List<Integer> supIds=new ArrayList<>();
        for (Map<String, Object> map : suppLiesId.get("suppLiesId")) {
            Integer siteId = (Integer) map.get("siteId");
            Integer supId = (Integer) map.get("supId");
            //添加到集合
            siteIds.add(siteId);
            supIds.add(supId);
        }
        boolean b = suppliesManagementService.deleteSupplies(supIds,siteIds);
        if (b) {
            return Result.ok(null, "删除成功");
        }
        return Result.failed("删除失败，稍后再试");
    }

    /**
     * 分页查询 全部物料
     *
     */
    @GetMapping
    @ApiOperation(value = "查询全部物料信息", notes = "查询所有的物料信息")
    public Result<List<SuppliesVO>> pageSelectSupplies() {
        return Result.ok(suppliesManagementService.pageSelectSupplies(),"查询成功");
    }

    /**
     * 模糊查询站点的物料信息
     *
     */
    @PostMapping(value = "/blurrySupplies")
    @ApiOperation(value = "通过物料名称模糊查询站点的物料信息", notes = "通过物料名称模糊查询站点的物料信息")
    public Result<List<SuppliesVO>> blurrySelectSupplies(@RequestBody SearchSupVo searchSup) {
        return Result.ok(suppliesManagementService
                .blurrySelectSupplies(searchSup.getSuppliesName(), searchSup.getSiteName()),"查询成功");
    }



}
